setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

## Import packages
require(cregg)
require(ggplot2)
require(haven)
require(psych)

#################
## Import Data ##
#################

## Experiment 1
dcj_exp1 <- readRDS("elecvspref_jan22_conjoint_v4.1.rds")

## Experiment 2
dcj_exp2 <- readRDS("elecvspref_mar16_conjoint_v4.1.rds")

##################
## Experiment 1 ##
##################

## Clean Data
dim(dcj_exp1)
## Experimental Assignment Mechanically Missing
dcj_exp1 <- subset(dcj_exp1, !is.na(dcj_exp1$candgender))
dim(dcj_exp1)
## Non-Responses
dcj_exp1 <- subset(dcj_exp1, !is.na(dcj_exp1$selected))
dim(dcj_exp1)

## Create Party Variable
table(dcj_exp1$candparty)
dcj_exp1$candparty_3cat <- 
  ifelse(dcj_exp1$candparty%in%c("(Party) LDP","(Party) Komeito"),1,
         ifelse(dcj_exp1$candparty%in%c("(Party) CDP","(Party) DPP",
                                        "(Party) Ishin","(Party) JCP"),2,
                ifelse(dcj_exp1$candparty%in%c("(Party) Independent"),3,NA)))
dcj_exp1$candparty_3cat <- 
  factor(dcj_exp1$candparty_3cat,
         labels=c("Ruling party","Opposition party","Independent"))
table(dcj_exp1$candparty_3cat, useNA = "always")

## Adjust Variables
dcj_exp1$dv <- 
  factor(ifelse(dcj_exp1$electability==1, "Expectation", "Preference"),
         levels = c("Preference", "Expectation"))
dcj_exp1$candgender <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp1$candgender),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp1$candgender)))
dcj_exp1$candparty <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp1$candparty),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp1$candparty)))
dcj_exp1$candage <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp1$candage),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp1$candage)))
dcj_exp1$candexperience <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp1$candexperience),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp1$candexperience)))
dcj_exp1$candedu <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp1$candedu),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp1$candedu)))
dcj_exp1$candmarriage <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp1$candmarriage),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp1$candmarriage)))
dcj_exp1$candkidage <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp1$candkidage),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp1$candkidage)))
dcj_exp1$candlivetype <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp1$candlivetype),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp1$candlivetype)))

## Variable Labels 
vlab_exp1 <- list("candgender"="Gender",
                  "candparty"="Party Affliation",
                  "candage"="Age",
                  "candexperience"="Political Experience",
                  "candedu"="Educational Attainment",
                  "candmarriage"="Marital Status",
                  "candkidage"="Youngest Child",
                  "candlivetype"="Residence")

## Marginal Means ##
resexp1_each <- cj(selected ~ candgender + #candparty + 
                     candage + 
                     candexperience + candedu + candmarriage + 
                     candkidage + candlivetype, 
                   data = dcj_exp1,
                   feature_labels = vlab_exp1,
                   by = ~dv+candparty_3cat, 
                   id = ~Response.ID, estimate="mm", h0=0.5)
head(resexp1_each)
resexp1_diff1 <- cj(selected ~ candgender + #candparty + 
                      candage + 
                      candexperience + candedu + candmarriage + 
                      candkidage + candlivetype, 
                    data = subset(dcj_exp1, candparty_3cat=="Ruling party"),
                    feature_labels = vlab_exp1,
                    by = ~dv, 
                    id = ~Response.ID, estimate="mm_diff")
resexp1_diff1$candparty_3cat <- "Ruling party"
head(resexp1_diff1)
resexp1_diff2 <- cj(selected ~ candgender + #candparty + 
                      candage + 
                      candexperience + candedu + candmarriage + 
                      candkidage + candlivetype, 
                    data = subset(dcj_exp1, candparty_3cat=="Opposition party"),
                    feature_labels = vlab_exp1,
                    by = ~dv, 
                    id = ~Response.ID, estimate="mm_diff")
resexp1_diff2$candparty_3cat <- "Opposition party"
head(resexp1_diff2)
resexp1_diff3 <- cj(selected ~ candgender + #candparty + 
                      candage + 
                      candexperience + candedu + candmarriage + 
                      candkidage + candlivetype, 
                    data = subset(dcj_exp1, candparty_3cat=="Independent"),
                    feature_labels = vlab_exp1,
                    by = ~dv, 
                    id = ~Response.ID, estimate="mm_diff")
resexp1_diff3$candparty_3cat <- "Independent"
head(resexp1_diff3)

resexp1_all <- rbind(resexp1_each,
                     resexp1_diff1,resexp1_diff2,resexp1_diff3)
resexp1_all$BY <- 
  factor(gsub("\\*\\*\\*.*$","",resexp1_all$BY), 
         levels = c("Preference","Expectation",              
                    "Expectation - Preference"))
resexp1_all$candparty_3cat <- 
  factor(resexp1_all$candparty_3cat, 
         levels = c("Ruling party","Opposition party","Independent"))
resexp1_all$level <- factor(resexp1_all$level, 
                             levels = rev(levels(resexp1_all$level)))

setfacetexp1_vline <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             VL = rep(c(0.5,0.5,0),each=2))
setfacetexp1_lims <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             level = factor("Female", levels(resexp1_all$level)), 
             feature = factor("Gender", levels(resexp1_all$feature)),
             lims = c(0.3,0.7,0.3,0.7,-0.15,0.15))

## Plot!
## Figure A29 (Appendix F.3)
require(ggplot2)
p_resexp1 <- 
  ggplot(resexp1_all) + 
  geom_vline(data=setfacetexp1_vline, aes(xintercept=VL), 
             linetype=2, color="gray70") + 
  geom_errorbarh(aes(xmin=lower,xmax=upper,y=level, 
                     color=candparty_3cat), height=0, linewidth = 0.5,
                 position = position_dodge(width=-0.8)) +
  geom_point(aes(x=estimate,y=level,
                 shape=candparty_3cat), size=1,
             position = position_dodge(width=-0.8)) + 
  geom_point(data=setfacetexp1_lims, aes(x=lims,y=level), 
             size=1, alpha=0) + 
  scale_shape_discrete(name="Candidate's Party") + 
  scale_color_discrete(name="Candidate's Party") + 
  facet_grid(feature~BY, scales = "free", space = "free") + 
  labs(x="Marginal Means Estimates (with 95% Confidence Intervals)",
       y=NULL,
       caption="Ruling parties are LDP and Komeito; Opposition parties are CDP, DPP, Ishin, and JCP.") + 
  theme_bw(base_family = "serif") + 
  theme(strip.text.y = element_text(angle=0),
        plot.title = element_text(hjust=0.5), 
        legend.position = "bottom")
p_resexp1
ggsave("resexp1_candparty_v4.pdf", width=8, height=7)

### Extracting p-values
### Figure A30 (Appendix F.3)
require(ggplot2)
p_resexp1_pval <- 
  ggplot(resexp1_all) + 
  geom_label(aes(x=ifelse(candparty_3cat=="Ruling party",0.485,
                          ifelse(candparty_3cat=="Opposition party",0.50,0.515)),
                 y=level,label=sprintf("%.03f",p), 
                 fill=candparty_3cat), size=3) + 
  geom_point(aes(x=0.48,y=level), size=1, alpha=0) + 
  geom_point(aes(x=0.52,y=level), size=1, alpha=0) + 
  scale_fill_discrete(name="Candidate's Party") + 
  facet_grid(feature~BY, scales = "free", space = "free") + 
  labs(x="p-values from significance test",
       y=NULL,
       caption="Note: H0=0.5 for preference and expectation tasks. H0=0 for preference-expecation gap.") + 
  theme_minimal(base_family = "serif") + 
  theme(strip.text.y = element_text(angle=0),
        plot.title = element_text(hjust=0.5), 
        legend.position = "bottom",
        axis.text.x = element_blank())
p_resexp1_pval
ggsave("resexp1_candparty_pval_v4.pdf", width=8, height=7)

## AMCE (only estimation) ##

resexp1_amce_each <- cj(selected ~ candgender + #candparty + 
                          candage + 
                          candexperience + candedu + candmarriage + 
                          candkidage + candlivetype, 
                        data = dcj_exp1,
                        feature_labels = vlab_exp1,
                        by = ~dv+candparty_3cat, 
                        id = ~Response.ID, estimate="amce")
head(resexp1_amce_each)
resexp1_amce_diff1 <- cj(selected ~ candgender + #candparty + 
                           candage + 
                           candexperience + candedu + candmarriage + 
                           candkidage + candlivetype, 
                         data = subset(dcj_exp1, candparty_3cat=="Ruling party"),
                         feature_labels = vlab_exp1,
                         by = ~dv, 
                         id = ~Response.ID, estimate="amce_diff")
resexp1_amce_diff1$candparty_3cat <- "Ruling party"
head(resexp1_amce_diff1)
resexp1_amce_diff2 <- cj(selected ~ candgender + #candparty + 
                           candage + 
                           candexperience + candedu + candmarriage + 
                           candkidage + candlivetype, 
                         data = subset(dcj_exp1, candparty_3cat=="Opposition party"),
                         feature_labels = vlab_exp1,
                         by = ~dv, 
                         id = ~Response.ID, estimate="amce_diff")
resexp1_amce_diff2$candparty_3cat <- "Opposition party"
head(resexp1_amce_diff2)
resexp1_amce_diff3 <- cj(selected ~ candgender + #candparty + 
                           candage + 
                           candexperience + candedu + candmarriage + 
                           candkidage + candlivetype, 
                         data = subset(dcj_exp1, candparty_3cat=="Independent"),
                         feature_labels = vlab_exp1,
                         by = ~dv, 
                         id = ~Response.ID, estimate="amce_diff")
resexp1_amce_diff3$candparty_3cat <- "Independent"
head(resexp1_amce_diff3)

resexp1_amce_all <- rbind(resexp1_amce_each,
                          resexp1_amce_diff1,resexp1_amce_diff2,resexp1_amce_diff3)
resexp1_amce_all$BY <- 
  factor(gsub("\\*\\*\\*.*$","",resexp1_amce_all$BY), 
         levels = c("Preference","Expectation",              
                    "Expectation - Preference"))
resexp1_amce_all$candparty_3cat <- 
  factor(resexp1_amce_all$candparty_3cat, 
         levels = c("Ruling party","Opposition party","Independent"))
resexp1_amce_all$level <- factor(resexp1_amce_all$level, 
                                  levels = rev(levels(resexp1_amce_all$level)))

##################
## Experiment 2 ##
##################

## Clean Data
dim(dcj_exp2)
## Experimental Assignment Mechanically Missing
dcj_exp2 <- subset(dcj_exp2, !is.na(dcj_exp2$candgender))
dim(dcj_exp2)
## Nonresponses
dcj_exp2 <- subset(dcj_exp2, !is.na(dcj_exp2$selected))
dim(dcj_exp2)

## Add Respondent Gender variable
dcj_exp2$candparty_3cat <- 
  ifelse(dcj_exp2$candparty%in%c("(Party) Ruling party"),1,
         ifelse(dcj_exp2$candparty%in%c("(Party) Opposition party"),2,
                ifelse(dcj_exp2$candparty%in%c("(Party) Independent"),3,NA)))
dcj_exp2$candparty_3cat <- 
  factor(dcj_exp2$candparty_3cat,
         labels=c("Ruling party","Opposition party","Independent"))
table(dcj_exp2$candparty_3cat)

## Adjust Variables
dcj_exp2$dv <- 
  factor(ifelse(dcj_exp2$electability==1, "Expectation", "Preference"),
         levels = c("Preference", "Expectation"))
dcj_exp2$candgender <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candgender),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candgender)))
dcj_exp2$candparty <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candparty),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candparty)))
dcj_exp2$candage <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candage),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candage)))
dcj_exp2$candexperience <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candexperience),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candexperience)))
dcj_exp2$candedu <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candedu),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candedu)))
dcj_exp2$candfam <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candfam),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candfam)))
dcj_exp2$candlivetype <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candlivetype),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candlivetype)))
dcj_exp2$candorigin <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candorigin),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candorigin)))
dcj_exp2$candimppol <- 
  factor(gsub("^\\(.*\\) ","", dcj_exp2$candimppol),
         levels = gsub("^\\(.*\\) ","", levels(dcj_exp2$candimppol)))

## Variable Labels 
vlab_exp2 <- list("candgender"="Gender",
                  "candparty"="Party Affliation",
                  "candage"="Age",
                  "candexperience"="Political Experience",
                  "candedu"="Educational Attainment",
                  "candfam"="Family",
                  "candorigin"="Native Place",
                  "candlivetype"="Residence",
                  "candimppol"="Policy Focus")

## house (Marginal Means) ##

table(dcj_exp2$conjoint_eleclevel,dcj_exp2$candparty_3cat)

resexp2_each_house <- cj(selected ~ candgender + #candparty + 
                           candage + 
                           candexperience + candedu + candfam + 
                           candlivetype + candorigin + candimppol, 
                         data = subset(dcj_exp2, conjoint_eleclevel=="house"),
                         feature_labels = vlab_exp2,
                         by = ~dv+candparty_3cat, 
                         id = ~Response.ID, estimate="mm", h0=0.5)
head(resexp2_each_house)
resexp2_diff_house1 <- cj(selected ~ candgender + #candparty + 
                            candage + 
                            candexperience + candedu + candfam + 
                            candlivetype + candorigin + candimppol, 
                          data = subset(dcj_exp2, conjoint_eleclevel=="house" & 
                                          candparty_3cat == "Ruling party"),
                          feature_labels = vlab_exp2,
                          by = ~dv, 
                          id = ~Response.ID, estimate="mm_diff")
resexp2_diff_house1$candparty_3cat <- "Ruling party"
head(resexp2_diff_house1)
resexp2_diff_house2 <- cj(selected ~ candgender + #candparty + 
                            candage + 
                            candexperience + candedu + candfam + 
                            candlivetype + candorigin + candimppol, 
                          data = subset(dcj_exp2, conjoint_eleclevel=="house" & 
                                          candparty_3cat == "Opposition party"),
                          feature_labels = vlab_exp2,
                          by = ~dv, 
                          id = ~Response.ID, estimate="mm_diff")
resexp2_diff_house2$candparty_3cat <- "Opposition party"
head(resexp2_diff_house2)
resexp2_diff_house3 <- cj(selected ~ candgender + #candparty + 
                            candage + 
                            candexperience + candedu + candfam + 
                            candlivetype + candorigin + candimppol, 
                          data = subset(dcj_exp2, conjoint_eleclevel=="house" & 
                                          candparty_3cat == "Independent"),
                          feature_labels = vlab_exp2,
                          by = ~dv, 
                          id = ~Response.ID, estimate="mm_diff")
resexp2_diff_house3$candparty_3cat <- "Independent"
head(resexp2_diff_house3)

resexp2_all_house <- rbind(resexp2_each_house,
                            resexp2_diff_house1,
                            resexp2_diff_house2,
                            resexp2_diff_house3)
resexp2_all_house$BY <- 
  factor(gsub("\\*\\*\\*.*$", "", resexp2_all_house$BY), 
         levels = c("Preference","Expectation",              
                    "Expectation - Preference"))
resexp2_all_house$candparty_3cat <- 
  factor(resexp2_all_house$candparty_3cat, 
         levels = c("Ruling party","Opposition party","Independent"))
resexp2_all_house$level <- 
  factor(resexp2_all_house$level, 
         levels = rev(levels(resexp2_all_house$level)))

setfacetexp2_vline_house <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             VL = rep(c(0.5,0.5,0),each=2))
setfacetexp2_lims_house <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             level = factor("Female", levels(resexp2_all_house$level)), 
             feature = factor("Gender", levels(resexp2_all_house$feature)),
             lims = c(0.3,0.7,0.3,0.7,-0.15,0.15))

## Plot!
## Figure A31 (Appendix F.3)
require(ggplot2)
p_resexp2_house <- 
  ggplot(resexp2_all_house) + 
  geom_vline(data=setfacetexp2_vline_house, aes(xintercept=VL), 
             linetype=2, color="gray70") + 
  geom_errorbarh(aes(xmin=lower,xmax=upper,y=level, 
                     color=candparty_3cat), height=0, linewidth = 0.5,
                 position = position_dodge(width=-0.8)) +
  geom_point(aes(x=estimate,y=level,
                 shape=candparty_3cat), size=1,
             position = position_dodge(width=-0.8)) + 
  geom_point(data=setfacetexp2_lims_house, aes(x=lims,y=level), 
             size=1, alpha=0) + 
  scale_shape_discrete(name="Candidate's Party") + 
  scale_color_discrete(name="Candidate's Party") + 
  # scale_color_manual(name="Women's Issue Minister", values=c("gray50","black",
  #                                                            "orange1","darkorange4")) + 
  # scale_shape_manual(name="Women's Issue Minister", values=c(16,16,17,17)) + 
  facet_grid(feature~BY, scales = "free", space = "free") + 
  labs(x="Marginal Means Estimates (with 95% Confidence Intervals)",
       y=NULL,
       caption="Ruling parties are LDP and Komeito; Opposition parties are CDP, DPP, Ishin, and JCP.") + 
  theme_bw(base_family = "serif") + 
  theme(strip.text.y = element_text(angle=0),
        plot.title = element_text(hjust=0.5), 
        legend.position = "bottom")
p_resexp2_house
ggsave("resexp2_house_candparty_v4.pdf", width=8, height=7)

### Extracting p-values
### Figure A32 (Appendix F.3)
require(ggplot2)
p_resexp2_house_pval <- 
  ggplot(resexp2_all_house) + 
  geom_label(aes(x=ifelse(candparty_3cat=="Ruling party",0.485,
                          ifelse(candparty_3cat=="Opposition party",0.50,0.515)),
                 y=level,label=sprintf("%.03f",p), 
                 fill=candparty_3cat), size=3) + 
  geom_point(aes(x=0.48,y=level), size=1, alpha=0) + 
  geom_point(aes(x=0.52,y=level), size=1, alpha=0) + 
  scale_fill_discrete(name="Respondent's Gender") + 
  facet_grid(feature~BY, scales = "free", space = "free") + 
  labs(x="p-values from significance test",
       y=NULL,
       caption="Note: H0=0.5 for preference and expectation tasks. H0=0 for preference-expecation gap.") + 
  theme_minimal(base_family = "serif") + 
  theme(strip.text.y = element_text(angle=0),
        plot.title = element_text(hjust=0.5), 
        legend.position = "bottom",
        axis.text.x = element_blank())
p_resexp2_house_pval
ggsave("resexp2_house_candparty_pval_v4.pdf", width=8, height=7.5)

## house (AMCE, only estimation) ##

resexp2_amce_each_house <- cj(selected ~ candgender + #candparty + 
                                candage + 
                                candexperience + candedu + candfam + 
                                candlivetype + candorigin + candimppol, 
                              data = subset(dcj_exp2, conjoint_eleclevel=="house"),
                              feature_labels = vlab_exp2,
                              by = ~dv+candparty_3cat, 
                              id = ~Response.ID, estimate="amce")
head(resexp2_amce_each_house)
resexp2_amce_diff_house1 <- cj(selected ~ candgender + #candparty + 
                                 candage + 
                                 candexperience + candedu + candfam + 
                                 candlivetype + candorigin + candimppol, 
                               data = subset(dcj_exp2, conjoint_eleclevel=="house" & 
                                               candparty_3cat=="Ruling party"),
                               feature_labels = vlab_exp2,
                               by = ~dv, 
                               id = ~Response.ID, estimate="amce_diff")
resexp2_amce_diff_house1$candparty_3cat <- "Ruling party"
head(resexp2_amce_diff_house1)
resexp2_amce_diff_house2 <- cj(selected ~ candgender + #candparty + 
                                 candage + 
                                 candexperience + candedu + candfam + 
                                 candlivetype + candorigin + candimppol, 
                               data = subset(dcj_exp2, conjoint_eleclevel=="house" & 
                                               candparty_3cat=="Opposition party"),
                               feature_labels = vlab_exp2,
                               by = ~dv, 
                               id = ~Response.ID, estimate="amce_diff")
resexp2_amce_diff_house2$candparty_3cat <- "Opposition party"
head(resexp2_amce_diff_house2)
resexp2_amce_diff_house3 <- cj(selected ~ candgender + #candparty + 
                                 candage + 
                                 candexperience + candedu + candfam + 
                                 candlivetype + candorigin + candimppol, 
                               data = subset(dcj_exp2, conjoint_eleclevel=="house" & 
                                               candparty_3cat=="Independent"),
                               feature_labels = vlab_exp2,
                               by = ~dv, 
                               id = ~Response.ID, estimate="amce_diff")
resexp2_amce_diff_house3$candparty_3cat <- "Independent"
head(resexp2_amce_diff_house3)

resexp2_amce_all_house <- rbind(resexp2_amce_each_house,
                                 resexp2_amce_diff_house1,
                                 resexp2_amce_diff_house2,
                                resexp2_amce_diff_house3)
resexp2_amce_all_house$BY <- 
  factor(gsub("\\*\\*\\*.*$","",resexp2_amce_all_house$BY), 
         levels = c("Preference","Expectation",              
                    "Expectation - Preference"))
resexp2_amce_all_house$candparty_3cat <- 
  factor(resexp2_amce_all_house$candparty_3cat, 
         levels = c("Ruling party","Opposition party","Independent"))
resexp2_amce_all_house$level <- 
  factor(resexp2_amce_all_house$level, 
         levels = rev(levels(resexp2_amce_all_house$level)))

## municipal (Marginal Means) ##

resexp2_each_municipal <- cj(selected ~ candgender + #candparty + 
                               candage + 
                               candexperience + candedu + candfam + 
                               candlivetype + candorigin + candimppol, 
                             data = subset(dcj_exp2, conjoint_eleclevel=="municipal"),
                             feature_labels = vlab_exp2,
                             by = ~dv+candparty_3cat, 
                             id = ~Response.ID, estimate="mm", h0=0.5)
head(resexp2_each_municipal)
resexp2_diff_municipal1 <- cj(selected ~ candgender + #candparty + 
                                candage + 
                                candexperience + candedu + candfam + 
                                candlivetype + candorigin + candimppol, 
                              data = subset(dcj_exp2, conjoint_eleclevel=="municipal" & 
                                              candparty_3cat == "Ruling party"),
                              feature_labels = vlab_exp2,
                              by = ~dv, 
                              id = ~Response.ID, estimate="mm_diff")
resexp2_diff_municipal1$candparty_3cat <- "Ruling party"
head(resexp2_diff_municipal1)
resexp2_diff_municipal2 <- cj(selected ~ candgender + #candparty + 
                                candage + 
                                candexperience + candedu + candfam + 
                                candlivetype + candorigin + candimppol, 
                              data = subset(dcj_exp2, conjoint_eleclevel=="municipal" & 
                                              candparty_3cat == "Opposition party"),
                              feature_labels = vlab_exp2,
                              by = ~dv, 
                              id = ~Response.ID, estimate="mm_diff")
resexp2_diff_municipal2$candparty_3cat <- "Opposition party"
head(resexp2_diff_municipal2)
resexp2_diff_municipal3 <- cj(selected ~ candgender + #candparty + 
                                candage + 
                                candexperience + candedu + candfam + 
                                candlivetype + candorigin + candimppol, 
                              data = subset(dcj_exp2, conjoint_eleclevel=="municipal" & 
                                              candparty_3cat == "Independent"),
                              feature_labels = vlab_exp2,
                              by = ~dv, 
                              id = ~Response.ID, estimate="mm_diff")
resexp2_diff_municipal3$candparty_3cat <- "Independent"
head(resexp2_diff_municipal3)

resexp2_all_municipal <- rbind(resexp2_each_municipal,
                                resexp2_diff_municipal1,
                                resexp2_diff_municipal2,
                               resexp2_diff_municipal3)
resexp2_all_municipal$BY <- 
  factor(gsub("\\*\\*\\*.*$", "", resexp2_all_municipal$BY), 
         levels = c("Preference","Expectation",              
                    "Expectation - Preference"))
resexp2_all_municipal$candparty_3cat <- 
  factor(resexp2_all_municipal$candparty_3cat, 
         levels = c("Ruling party","Opposition party","Independent"))
resexp2_all_municipal$level <- 
  factor(resexp2_all_municipal$level, 
         levels = rev(levels(resexp2_all_municipal$level)))

setfacetexp2_vline_municipal <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             VL = rep(c(0.5,0.5,0),each=2))
setfacetexp2_lims_municipal <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             level = factor("Female", levels(resexp2_all_municipal$level)), 
             feature = factor("Gender", levels(resexp2_all_municipal$feature)),
             lims = c(0.3,0.7,0.3,0.7,-0.15,0.15))

## Plot!
## Figure A33 (Appendix F.3)
require(ggplot2)
p_resexp2_municipal <- 
  ggplot(resexp2_all_municipal) + 
  geom_vline(data=setfacetexp2_vline_municipal, aes(xintercept=VL), 
             linetype=2, color="gray70") + 
  geom_errorbarh(aes(xmin=lower,xmax=upper,y=level, 
                     color=candparty_3cat), height=0, linewidth = 0.5,
                 position = position_dodge(width=-0.8)) +
  geom_point(aes(x=estimate,y=level,
                 shape=candparty_3cat), size=1,
             position = position_dodge(width=-0.8)) + 
  geom_point(data=setfacetexp2_lims_municipal, aes(x=lims,y=level), 
             size=1, alpha=0) + 
  scale_shape_discrete(name="Candidate's Party") + 
  scale_color_discrete(name="Candidate's Party") + 
  # scale_color_manual(name="Women's Issue Minister", values=c("gray50","black",
  #                                                            "orange1","darkorange4")) + 
  # scale_shape_manual(name="Women's Issue Minister", values=c(16,16,17,17)) + 
  facet_grid(feature~BY, scales = "free", space = "free") + 
  labs(x="Marginal Means Estimates (with 95% Confidence Intervals)",
       y=NULL) + 
  theme_bw(base_family = "serif") + 
  theme(strip.text.y = element_text(angle=0),
        plot.title = element_text(hjust=0.5), 
        legend.position = "bottom")
p_resexp2_municipal
ggsave("resexp2_municipal_candparty_v4.pdf", width=8, height=7)

### Extracting p-values
### Figure A34 (Appendix F.3)
require(ggplot2)
p_resexp2_municipal_pval <- 
  ggplot(resexp2_all_municipal) + 
  geom_label(aes(x=ifelse(candparty_3cat=="Ruling party",0.485,
                          ifelse(candparty_3cat=="Opposition party",0.50,0.515)),
                 y=level,label=sprintf("%.03f",p), 
                 fill=candparty_3cat), size=3) + 
  geom_point(aes(x=0.48,y=level), size=1, alpha=0) + 
  geom_point(aes(x=0.52,y=level), size=1, alpha=0) + 
  scale_fill_discrete(name="Respondent's Gender") + 
  facet_grid(feature~BY, scales = "free", space = "free") + 
  labs(x="p-values from significance test",
       y=NULL,
       caption="Note: H0=0.5 for preference and expectation tasks. H0=0 for preference-expecation gap.") + 
  theme_minimal(base_family = "serif") + 
  theme(strip.text.y = element_text(angle=0),
        plot.title = element_text(hjust=0.5), 
        legend.position = "bottom",
        axis.text.x = element_blank())
p_resexp2_municipal_pval
ggsave("resexp2_municipal_candparty_pval_v4.pdf", width=8, height=7.5)

## municipal (AMCE, only estimation) ##

resexp2_amce_each_municipal <- cj(selected ~ candgender + #candparty + 
                                    candage + 
                                    candexperience + candedu + candfam + 
                                    candlivetype + candorigin + candimppol, 
                                  data = subset(dcj_exp2, conjoint_eleclevel=="municipal"),
                                  feature_labels = vlab_exp2,
                                  by = ~dv+candparty_3cat, 
                                  id = ~Response.ID, estimate="amce")
head(resexp2_amce_each_municipal)
resexp2_amce_diff_municipal1 <- cj(selected ~ candgender + #candparty + 
                                     candage + 
                                     candexperience + candedu + candfam + 
                                     candlivetype + candorigin + candimppol, 
                                   data = subset(dcj_exp2, conjoint_eleclevel=="municipal" & 
                                                   candparty_3cat=="Ruling party"),
                                   feature_labels = vlab_exp2,
                                   by = ~dv, 
                                   id = ~Response.ID, estimate="amce_diff")
resexp2_amce_diff_municipal1$candparty_3cat <- "Ruling party"
head(resexp2_amce_diff_municipal1)
resexp2_amce_diff_municipal2 <- cj(selected ~ candgender + #candparty + 
                                     candage + 
                                     candexperience + candedu + candfam + 
                                     candlivetype + candorigin + candimppol, 
                                   data = subset(dcj_exp2, conjoint_eleclevel=="municipal" & 
                                                   candparty_3cat=="Opposition party"),
                                   feature_labels = vlab_exp2,
                                   by = ~dv, 
                                   id = ~Response.ID, estimate="amce_diff")
resexp2_amce_diff_municipal2$candparty_3cat <- "Opposition party"
head(resexp2_amce_diff_municipal2)
resexp2_amce_diff_municipal3 <- cj(selected ~ candgender + #candparty + 
                                     candage + 
                                     candexperience + candedu + candfam + 
                                     candlivetype + candorigin + candimppol, 
                                   data = subset(dcj_exp2, conjoint_eleclevel=="municipal" & 
                                                   candparty_3cat=="Independent"),
                                   feature_labels = vlab_exp2,
                                   by = ~dv, 
                                   id = ~Response.ID, estimate="amce_diff")
resexp2_amce_diff_municipal3$candparty_3cat <- "Independent"
head(resexp2_amce_diff_municipal3)

resexp2_amce_all_municipal <- rbind(resexp2_amce_each_municipal,
                                     resexp2_amce_diff_municipal1,
                                     resexp2_amce_diff_municipal2,
                                    resexp2_amce_diff_municipal3)
resexp2_amce_all_municipal$BY <- 
  factor(gsub("\\*\\*\\*.*$","",resexp2_amce_all_municipal$BY), 
         levels = c("Preference","Expectation",              
                    "Expectation - Preference"))
resexp2_amce_all_municipal$candparty_3cat <- 
  factor(resexp2_amce_all_municipal$candparty_3cat, 
         levels = c("Ruling party","Opposition party","Independent"))
resexp2_amce_all_municipal$level <- 
  factor(resexp2_amce_all_municipal$level, 
         levels = rev(levels(resexp2_amce_all_municipal$level)))

###############################
## Figure A26 (Appendix F.1) ##
###############################

tmp1 <- subset(resexp1_all, feature=="Gender")
tmp1$exp <- "Experiment 1\n[Without Policy Attribute]\n(House of Representatives)"
tmp2a <- subset(resexp2_all_house, feature=="Gender")
tmp2a$exp <- "Experiment 2\n[With Policy Attribute]\n(House of Representatives)"
tmp2b <- subset(resexp2_all_municipal, feature=="Gender")
tmp2b$exp <- "Experiment 2\n[With Policy Attribute]\n(Municipal Council)"

fig2dt <- rbind(tmp1, tmp2a, tmp2b)
fig2dt$exp <- factor(fig2dt$exp, unique(fig2dt$exp))

setfacetfig2_vline <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             VL = rep(c(0.5,0.5,0),each=2))
setfacetfig2_lims <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             level = factor("Female", levels(fig2dt$level)), 
             feature = factor("Gender", levels(fig2dt$feature)),
             lims = c(0.45,0.55,0.45,0.55,-0.06,0.06))

## Plot!
require(ggplot2)
p_resfig2_candparty <- 
  ggplot(fig2dt) + 
  geom_vline(data=setfacetfig2_vline, aes(xintercept=VL), 
             linetype=2, color="gray70") + 
  geom_errorbarh(aes(xmin=lower,xmax=upper,y=level, color=candparty_3cat), 
                 height=0, linewidth=0.5,
                 position=position_dodge(width=-0.8)) +
  geom_point(aes(x=estimate,y=level, shape=candparty_3cat), size=1.5,
             position=position_dodge(width=-0.8)) + 
  geom_point(data=setfacetfig2_lims, aes(x=lims,y=level), 
             size=1, alpha=0) + 
  scale_shape_discrete(name="Candidate's Party") + 
  scale_color_manual(name="Candidate's Party", values=rep("black",3)) + 
  facet_grid(exp~BY, scales = "free", 
             switch = "y") + 
  scale_x_continuous(breaks = c(-0.05,0,0.05,0.47,0.5,0.53)) + 
  labs(x="Marginal Means Estimates (with 95% Confidence Intervals)",
       y=NULL,
       caption="Note: In Experiment 1, ruling parties are LDP and Komeito; Opposition parties are CDP, DPP, Ishin, and JCP.") + 
  theme_bw(base_family = "serif") + 
  theme(plot.title = element_text(hjust=0.5), 
        legend.position = "bottom",
        panel.grid = element_blank(),
        strip.placement = "outside",
        strip.text.y.left = element_text(angle=0),
        strip.background.y = element_blank())
p_resfig2_candparty
ggsave("fig2_candparty_v4.pdf", width=7, height=3.5)

#########################################
## Appendix F.2 = Figure A26 AMCE ver. ##
#########################################

tmp1 <- subset(resexp1_amce_all, feature=="Gender")
tmp1$exp <- "Experiment 1\n[Without Policy Attribute]\n(House of Representatives)"
tmp2a <- subset(resexp2_amce_all_house, feature=="Gender")
tmp2a$exp <- "Experiment 2\n[With Policy Attribute]\n(House of Representatives)"
tmp2b <- subset(resexp2_amce_all_municipal, feature=="Gender")
tmp2b$exp <- "Experiment 2\n[With Policy Attribute]\n(Municipal Council)"

fig2_amcedt <- rbind(tmp1, tmp2a, tmp2b)
fig2_amcedt$exp <- factor(fig2_amcedt$exp, unique(fig2_amcedt$exp))

setfacetfig2_amce_vline <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             VL = rep(c(0,0,0),each=2))
setfacetfig2_amce_lims <- 
  data.frame(BY = factor(rep(c("Preference","Expectation",              
                               "Expectation - Preference"), 
                             each = 2),
                         levels = 
                           c("Preference","Expectation",              
                             "Expectation - Preference")),
             level = factor("Female", levels(fig2_amcedt$level)), 
             feature = factor("Gender", levels(fig2_amcedt$feature)),
             lims = c(-0.08,0.08,-0.08,0.08,-0.14,0.02))
fig2_amcedt

## Plot!
## Figure A27 (Appendix F.2)
require(ggplot2)
p_resfig2_amce <- 
  ggplot(fig2_amcedt) + 
  geom_vline(data=setfacetfig2_amce_vline, aes(xintercept=VL), 
             linetype=2, color="gray70") + 
  geom_errorbarh(aes(xmin=lower,xmax=upper,y=level, color=candparty_3cat), 
                 height=0, linewidth=0.5,
                 position=position_dodge(width=-0.8)) +
  geom_point(aes(x=estimate,y=level, shape=candparty_3cat), size=1.5,
             position=position_dodge(width=-0.8)) + 
  geom_point(data=setfacetfig2_amce_lims, aes(x=lims,y=level), 
             size=1, alpha=0) + 
  scale_shape_discrete(name="Candidate's Party") + 
  scale_color_manual(name="Candidate's Party", values=rep("black",3)) + 
  facet_grid(exp~BY, scales = "free", 
             switch = "y") + 
  scale_x_continuous(breaks = c(-0.1,-0.05,0,0.05)) + 
  labs(x="Average Marginal Component Effect (with 95% Confidence Intervals)",
       y=NULL,
       caption="Note: In Experiment 1, ruling parties are LDP and Komeito; Opposition parties are CDP, DPP, Ishin, and JCP.") + 
  theme_bw(base_family = "serif") + 
  theme(plot.title = element_text(hjust=0.5), 
        legend.position = "bottom",
        panel.grid = element_blank(),
        strip.placement = "outside",
        strip.text.y.left = element_text(angle=0),
        strip.background.y = element_blank())
p_resfig2_amce
ggsave("fig2_candparty_amce_v4.pdf", width=7, height=3.5)

### Extracting p-values
### Figure A28 (Appendix F.2)
require(ggplot2)
p_resfig2_amce_pval <- 
  ggplot(fig2_amcedt) + 
  geom_label(aes(x=ifelse(candparty_3cat=="Ruling party",0.485,
                          ifelse(candparty_3cat=="Opposition party",0.50,0.515)),
                 y=level,label=sprintf("%.03f",p), 
                 fill=candparty_3cat), size=3) + 
  geom_point(aes(x=0.48,y=level), size=1, alpha=0) + 
  geom_point(aes(x=0.52,y=level), size=1, alpha=0) + 
  scale_fill_discrete(name="Candidate's Party") + 
  facet_grid(exp~BY, scales = "free", 
             switch = "y") + 
  labs(x="p-values from significance test (H0=0)",
       y=NULL) + 
  theme_minimal(base_family = "serif") + 
  theme(plot.title = element_text(hjust=0.5), 
        legend.position = "bottom",
        strip.placement = "outside",
        strip.text.y.left = element_text(angle=0),
        strip.background.y = element_blank(),
        axis.text.x = element_blank())
p_resfig2_amce_pval
ggsave("fig2_candparty_amce_pval_v4.pdf", width=7, height=3.5)
